package com.kingdee.eas.crm.market.app;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.crm.market.IsFinishEnum;
import com.kingdee.eas.crm.market.MarketPlanEntryCollection;
import com.kingdee.eas.crm.market.MarketPlanEntryFactory;
import com.kingdee.eas.crm.market.MarketPlanEntryInfo;
import com.kingdee.eas.crm.market.MarketPlanEntrySellPlanEntryCollection;
import com.kingdee.eas.crm.market.MarketPlanEntrySellPlanEntryFactory;
import com.kingdee.eas.crm.market.MarketPlanEntrySellPlanEntryInfo;
import com.kingdee.eas.crm.market.MarketSchemeEntryInfo;
import com.kingdee.eas.crm.market.MarketSchemeInfo;
import com.kingdee.eas.crm.market.ThemeEnum;
import com.kingdee.eas.fdc.basedata.FDCBillInfo;

public class MarketSchemeControllerBean extends AbstractMarketSchemeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.crm.market.app.MarketSchemeControllerBean");
    protected void checkNameDup(Context context, FDCBillInfo fdcbillinfo)
    throws BOSException, EASBizException
{
}

	protected void updateEnterpriseState(Context ctx, IObjectValue model, boolean isDel)
	    throws EASBizException, BOSException
	{
	/*  53*/        MarketSchemeInfo billInfo = (MarketSchemeInfo)model;
	/*  54*/        MarketPlanEntrySellPlanEntryCollection espc = new MarketPlanEntrySellPlanEntryCollection();
	/*  55*/        SelectorItemCollection updateSel = new SelectorItemCollection();
	/*  56*/        updateSel.add("state");
	/*  57*/        updateSel.add("esEntryId");
	/*  59*/        Set set_zhuti = new HashSet();
	/*  63*/        for(int i = 0; i < billInfo.getEntry().size(); i++)
	    {
	/*  64*/            MarketSchemeEntryInfo entry = billInfo.getEntry().get(i);
	/*  65*/            MarketPlanEntrySellPlanEntryInfo sellPlan = MarketPlanEntrySellPlanEntryFactory.getLocalInstance(ctx).getMarketPlanEntrySellPlanEntryInfo(new ObjectUuidPK(entry.getSellPlanID()));
	/*  67*/            if(entry.getIsEnd().equals(IsFinishEnum.FINISH))
	/*  68*/                sellPlan.setState(ThemeEnum.Finish);
	/*  69*/            else
	/*  69*/            if(entry.getIsEnd().equals(IsFinishEnum.CANCEL))
	/*  70*/                sellPlan.setState(ThemeEnum.Canceled);
	/*  72*/            else
	/*  72*/                sellPlan.setState(ThemeEnum.UnStarted);
	/*  74*/            sellPlan.setEsEntryId(entry.getId());
	/*  75*/            MarketPlanEntrySellPlanEntryFactory.getLocalInstance(ctx).updatePartial(sellPlan, updateSel);
	/*  76*/            espc.add(sellPlan);
	/*  77*/            if(!set_zhuti.contains(sellPlan.getHead().getId()))
	/*  78*/                set_zhuti.add(sellPlan.getHead().getId());
	    }
	
	/*  82*/        for(Iterator it = set_zhuti.iterator(); it.hasNext();)
	    {
	/*  84*/            int unstrat = 0;
	/*  85*/            int finish = 0;
	/*  86*/            int cancel = 0;
	/*  87*/            int count = 0;
	/*  88*/            Object object = it.next();
	/*  89*/            if(object != null)
	        {
	/*  92*/                for(int i = 0; i < espc.size(); i++)
	            {
	/*  93*/                    MarketPlanEntrySellPlanEntryInfo sellPlan = espc.get(i);
	/*  94*/                    if(object.equals(sellPlan.getHead().getId()))
	                {
	/*  95*/                        if(sellPlan.getState().getAlias().equals(IsFinishEnum.UNSTART.getAlias()))
	/*  96*/                            unstrat++;
	/*  97*/                        else
	/*  97*/                        if(sellPlan.getState().getAlias().equals(IsFinishEnum.FINISH.getAlias()))
	/*  98*/                            finish++;
	/*  99*/                        else
	/*  99*/                        if(sellPlan.getState().getAlias().equals(IsFinishEnum.CANCEL.getAlias()))
	/* 100*/                            cancel++;
	/* 102*/                        count++;
	                }
	            }
	
	/* 105*/                MarketPlanEntryInfo epei = new MarketPlanEntryInfo();
	/* 106*/                epei.setId(BOSUuid.read(object.toString()));
	/* 107*/                if(unstrat == count)
	/* 108*/                    epei.setState(ThemeEnum.UnStarted);
	/* 109*/                else
	/* 109*/                if(finish == count)
	/* 110*/                    epei.setState(ThemeEnum.Finish);
	/* 111*/                else
	/* 111*/                if(cancel == count)
	/* 112*/                    epei.setState(ThemeEnum.Canceled);
	/* 113*/                else
	/* 113*/                if(cancel + finish == count)
	/* 114*/                    epei.setState(ThemeEnum.Finish);
	/* 115*/                else
	/* 115*/                if(cancel + unstrat == count)
	/* 116*/                    epei.setState(ThemeEnum.UnStarted);
	/* 118*/                else
	/* 118*/                    epei.setState(ThemeEnum.Underway);
	/* 120*/                MarketPlanEntryFactory.getLocalInstance(ctx).updatePartial(epei, updateSel);
	        }
	    }
	
	}
	
	protected void _submit(Context ctx, IObjectPK pk, IObjectValue model)
	    throws BOSException, EASBizException
	{
	/* 124*/        super._submit(ctx, pk, model);
	/* 125*/        updateEnterpriseState(ctx, model, false);
	}
	
	protected IObjectPK _submit(Context ctx, IObjectValue model)
	    throws BOSException, EASBizException
	{
	/* 129*/        IObjectPK pk = super._submit(ctx, model);
	/* 130*/        updateEnterpriseState(ctx, model, false);
	/* 131*/        return pk;
	}
	
	protected MarketPlanEntryCollection getMarketPlanEntry(Context ctx, Set sellPlan)
	    throws BOSException
	{
	/* 134*/        EntityViewInfo evInfo = new EntityViewInfo();
	/* 135*/        FilterInfo filter = new FilterInfo();
	/* 136*/        filter.getFilterItems().add(new FilterItemInfo("palnTheme.id", sellPlan, CompareType.INCLUDE));
	/* 137*/        evInfo.setFilter(filter);
	/* 139*/        MarketPlanEntryCollection col = MarketPlanEntryFactory.getLocalInstance(ctx).getMarketPlanEntryCollection(evInfo);
	/* 141*/        return col;
	}
	
	protected void _delete(Context ctx, IObjectPK pk)
	    throws BOSException, EASBizException
	{
	/* 147*/        super._delete(ctx, pk);
	}
	
	protected void _delete(Context ctx, IObjectPK arrayPK[])
	    throws BOSException, EASBizException
	{
	/* 155*/        super._delete(ctx, arrayPK);
	}
}